import sqlite3


def dict_factory(cursor, row):
    d = {}
    for idx, col in enumerate(cursor.description):
        d[col[0]] = row[idx]
    return d


class Dao:
    def __init__(self, dbname='zpf.db'):
        self.conn = sqlite3.connect(dbname)
        self.conn.row_factory = dict_factory

    def query(self, sql, param=[]):
        c = self.conn.cursor()
        c.execute(sql, param)
        res = c.fetchall()
        c.close()
        return res

    def batch(self, sql_list):
        c = self.conn.cursor()
        try:
            for sql in sql_list:
                c.execute(sql)
            self.conn.commit()
        except Exception as error:
            print('数据库处理异常->', error)
            return False
        finally:
            c.close()
        return True

    def exec(self, sql):
        c = self.conn.cursor()
        try:
            c.execute(sql)
            self.conn.commit()
        except Exception as error:
            print('数据库处理异常->', error)
            return False
        finally:
            c.close()
        return True


if __name__ == "__main__":
    dao = Dao()
    print(dao.query('select * from t_cycle'))
